package com.htc.callcare.database;


import com.htc.callcare.model.NoteModel;
import com.htc.callcare.model.recordModel;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.content.ContentValues;

public class Database extends SQLiteOpenHelper{
	private static final int DB_VERSION = 1;
	private static String DB_NAME = "Call_Care_DB";
	private static String DB_TABLE = "Call_Care_Table";
	private static String DB_NOTE_TABLE = "Note_Table";
	
	private static final String ID = "id";
	private static final String CALL_TYPE = "callType";
	private static final String CALL_TIME = "callTime";
	private static final String CALL_DAY = "callDay";
	private static final String CALL_NUMBER = "callNumber";
	private static final String NOTE = "note";
	private static final String IS_CHECK = "isCheck";
	private static final String FILE_URI = "fileUri";
	
	private static final String NOTE_ID = "id";
	private static final String NOTE_CONTENT = "noteContent";
	private static final String NOTE_FILR_URI = "fileUri";
	
	
	private static final String CREATE_DB_TABLE = "CREATE TABLE "
			+ DB_TABLE + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CALL_TYPE
			+ " INTEGER, " + CALL_TIME + " TEXT, " + CALL_DAY + " TEXT, "
			+ CALL_NUMBER + " TEXT, " + NOTE + " TEXT, "
			+ IS_CHECK + " INTEGER, "+ FILE_URI + " TEXT);";
	
	private static final String CREATE_NOTE_TABLE = "CREATE TABLE "
			+ DB_NOTE_TABLE + " (" + NOTE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_CONTENT + " TEXT, "
			+ NOTE_FILR_URI + " TEXT);";

	public Database(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		Log.v("SQL", "Creating database");
		db.execSQL(CREATE_DB_TABLE);
		db.execSQL(CREATE_NOTE_TABLE);
		Log.v("SQL", "Creating database sucessfully");
		onUpgrade(db, db.getVersion(), DB_VERSION);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}

	public boolean addNote(NoteModel model){
		final SQLiteDatabase db = getWritableDatabase();
		final ContentValues values = new ContentValues();
		values.put(NOTE_CONTENT, model.getNoteContent());
		values.put(NOTE_FILR_URI, model.getFileUri());
		try {
			db.insert(DB_NOTE_TABLE, null, values);
			db.close();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			db.close();
			return false;
		}	
	}
	
	public String findNote(String fileUri){
		String result = "";
		final SQLiteDatabase db = getReadableDatabase();
		final String[] columns = new String[] { NOTE_ID, NOTE_CONTENT, NOTE_FILR_URI};
		final String whereClause = NOTE_FILR_URI + "=?";
		final String[] whereArgs = new String[] {String.valueOf(fileUri)};
		final Cursor cursor = db.query(DB_NOTE_TABLE, columns, whereClause,whereArgs, null, null, null);
		while (cursor.moveToNext()){
			result = cursor.getString(cursor.getColumnIndex(NOTE_CONTENT));
		}
		cursor.close();
		db.close();
		return result;
	}
	
	public boolean updateNote(String fileUri, String newContent){
		final SQLiteDatabase db = getReadableDatabase();
		final String whereClause = NOTE_FILR_URI +"=?";
		final String[] whereArgs = new String[]{fileUri};
		ContentValues contentValues = new ContentValues();
		contentValues.put(NOTE_CONTENT, newContent);
		db.update(DB_NOTE_TABLE, contentValues, whereClause, whereArgs);
		return false;
	}
	
	public boolean addCheckItemToDatabase(recordModel model){
		final SQLiteDatabase db = getWritableDatabase();
		final ContentValues values = new ContentValues();
		values.put(CALL_TYPE, model.getCallType());
		values.put(CALL_TIME, model.getCallType());
		values.put(CALL_DAY, model.getCallDay());
		values.put(CALL_NUMBER, model.callNumber);
		values.put(NOTE, model.getNote());
		values.put(IS_CHECK, 1);
		values.put(FILE_URI, model.getFileUri());
		try {
			db.insert(DB_TABLE, null, values);
			db.close();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			db.close();
			return false;
		}	
	}
	
	//check Item if it is checked?
	//true if is check
	//else not check
	public boolean getCheckItem(String fileUri){
		final SQLiteDatabase db = getReadableDatabase();
		final String[] columns = new String[] { ID, CALL_TYPE, CALL_TIME, CALL_DAY,
				CALL_NUMBER, NOTE, IS_CHECK, FILE_URI};
		final String whereClause = FILE_URI + "=?";
		final String[] whereArgs = new String[] {String.valueOf(fileUri)};
		final Cursor cursor = db.query(DB_TABLE, columns, whereClause,
				whereArgs, null, null, null);
		if (cursor.getCount() != 0) {
				return true;
		}
		cursor.close();
		db.close();
		return false;
	}
	
	//DeleteCheckItem
	public void deleteCheckItem(String fileUri) {
		final SQLiteDatabase db = getWritableDatabase();
		final String whereClause = FILE_URI + "=?";
		final String[] whereArgs = new String[] { String.valueOf(fileUri) };
		db.delete(DB_TABLE, whereClause, whereArgs);
		db.close();
	}
	
}
